package com.szqhyxj.youxiangju.biz.set;

import android.util.Log;

import com.szqhyxj.youxiangju.BaseConsumer;
import com.szqhyxj.youxiangju.BasePresenterImpl;
import com.szqhyxj.youxiangju.biz.core.TokenManager;
import com.szqhyxj.youxiangju.biz.core.UserInfoManger;
import com.szqhyxj.youxiangju.biz.update.DownloadUtil;
import com.szqhyxj.youxiangju.biz.update.UpdateModel;
import com.szqhyxj.youxiangju.biz.update.bean.UpdateInfo;
import com.szqhyxj.youxiangju.sys.BaseResult;
import com.szqhyxj.youxiangju.sys.util.ApkUtil;

import java.io.File;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2018/11/2.
 */

public class SettingPresenter extends BasePresenterImpl implements SettingContract.Presenter , ApkUtil.DownloadListener{
    SettingContract.View view;
    private UpdateModel.UpdateEntity updateEntity = new UpdateModel.UpdateEntity();
    private UpdateInfo data;
    private String TAG = "SettingPresenter";

    public SettingPresenter(SettingContract.View view) {
        this.view = view;
    }

    @Override
    public void out() {

        startRequest(SettingModel.logout()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<Object>>() {
                    @Override
                    public void accept(BaseResult<Object> baseResult) throws Exception {
                        if (baseResult.businessLogicSuccess()) {
                            view.showOut();
                            TokenManager.INSTANCE.clear();
                            UserInfoManger.INSTANCE.clear();
                        }else {
                            view.showToast(baseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage());
                        view.showOutFail();
                    }
                }));
    }


    @Override
    public void setVersionNumber(int versionNumber){
        updateEntity.setVersionNumber(versionNumber);
    }

    @Override
    public void setCheck(String check){
        updateEntity.setCheck(check);
    }



    @Override
    public void verifyAPK(){
        view.showLoading();
        startRequest(UpdateModel.checkUpdate(updateEntity.getVersionNumber()
                ,updateEntity.getCheck()).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<UpdateInfo>(){

                    @Override
                    public void error(String code, String msg){
                        view.dismissLoading();
                        view.showToast(msg);
                    }

                    @Override
                    protected void successAccept(UpdateInfo updateInfo){
                        view.dismissLoading();
                        data = updateInfo;
                        if(updateEntity.getVersionNumber() < data.getVersionNumber()){
                            view.showUpdate(data);
                        }else {
                            view.showToast("当前已是最新版本");
                        }
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.dismissLoading();
                        view.showToast(throwable.getMessage());
                    }
                }));
    }

    @Override
    public void downloadAPK(final File file, String url){
        System.out.println(url);
        DownloadUtil.get().download1(url, file, new DownloadUtil.OnDownloadListener() {
            @Override
            public void onDownloadSuccess() {
                Log.i(TAG, "onDownloadSuccess: ");
                view.showDownCompleted(file.getAbsolutePath());
            }

            @Override
            public void onDownloading(int progress) {
                Log.i(TAG, "onDownloading: " + progress);
                view.showDownProgress(progress);
            }

            @Override
            public void onDownloadFailed() {
                Log.i(TAG, "onDownloadFailed: ");
                view.showDownloadFail();
            }
        });
    }

    @Override
    public void init() {
        if (TokenManager.INSTANCE.getMToken()==null||TokenManager.INSTANCE.getMToken().equals("")){
            view.enshowout();
        }else{
            view.showout();
        }
    }

    @Override
    public void onStart(){
    }

    @Override
    public void onProgress(long totalBytes, long soFarBytes){
    }

    @Override
    public void onFinish(String localPath){
        view.showDownCompleted(localPath);
    }

    @Override
    public void onFailure(){
    }
}